/**
 *
 * jcrt and its source-code is licensed under the LGPL.
 * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
 * When copying, adapting, or redistributing this document in keeping with the guidelines above,
 * you are required to provide proper attribution to obinary.
 * If you reproduce or distribute the document without making any substantive modifications to its content,
 * please use the following attribution line:
 *
 * Copyright 2007 Atjava.com Ltd. (http://www.atjava.com) All rights reserved.
 *
 */
package com.atjava.jcrweb.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.atjava.jcrweb.JcrWebException;
import com.atjava.jcrweb.manager.PackingToFlexHelper;

/**
 * TreeServlet
 * 
 * @author biggie
 * @version $Revision: 1.7 $ $Date: 2008/10/14 06:47:32 $
 */
public abstract class FlexBaseServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	
	/**
	 * Log4j
	 */
	private Logger log = Logger.getLogger(this.getClass().getName());

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		execute(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		execute(request, response);
	}

	private void execute(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		Enumeration e = request.getParameterNames();
		Map<String, String[]> map = new HashMap<String, String[]>();
		while (e.hasMoreElements()) {
			String name = (String) e.nextElement();
			String[] values = request.getParameterValues(name);
			map.put(name, values);
			log.debug("Input Data -> name = " + name + " values = " + values[0]
					+ "\n");
		}
		try {
			String result = this.process(request, response, map);
			if (result != null) {
				log.debug("Result String:" + result);
				output(response, result);
			}
		} catch (JcrWebException e1) {
			log.error(e1.getMessage(),e1);
			PackingToFlexHelper pfh = new PackingToFlexHelper();
			pfh.setIsError(true);
			pfh.setErrorMessage(e1.getMessage());
			output(response, pfh.toString());
		} catch (Exception e2) {
			log.error(e2.getMessage(),e2);
			PackingToFlexHelper pfh = new PackingToFlexHelper();
			pfh.setIsError(true);
			pfh.setErrorMessage(e2.getMessage());
			output(response, pfh.toString());
		}
	}

	public abstract String process(HttpServletRequest request,
			HttpServletResponse response, Map<String, String[]> inputdata)
			throws IOException, ServletException, JcrWebException, Exception;

	private void output(HttpServletResponse response, String outstr)
			throws IOException, ServletException {
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.write(outstr);
	}

	public String getData(Map<String, String[]> inputdata, String key)
			throws UnsupportedEncodingException {
		String[] values = inputdata.get(key);
		if (values != null && values.length > 0) {
			return java.net.URLDecoder.decode(values[0], "UTF-8");
		}
		return "";
	}
}
